package com.teamin.service.labelService;

import com.alibaba.fastjson.JSONObject;
import com.teamin.entity.LabelEntity;
import com.teamin.entity.LabelTaskEntity;
import com.teamin.mapper.LabelMapper;
import com.teamin.service.CircleService;
import com.teamin.service.LogService;
import com.teamin.service.TaskService;
import com.teamin.service.WebsocketService;
import com.teamin.service.impl.LogServiceImpl;
import com.teamin.service.impl.WebsocketServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class LabelServiceImpl implements LabelService {


	@Autowired
	LabelMapper labelMapper;

	@Autowired
	private LogServiceImpl logService;

	@Autowired
	private TaskService taskServer;

	@Autowired
	CircleService circleService;

	@Autowired
	private WebsocketServiceImpl websocketService ;

	@Override
	public int deleteByPrimaryKey(Long circleId,Long labelId,Long controlUser) {
		LabelEntity lab = selectByPrimaryKey(labelId);
		int res =  labelMapper.deleteByPrimaryKey(labelId);
		if(res !=0 && lab != null) {
			//删除任务中关联的标签
			labelMapper.deleteLabelByTask(labelId) ;
			String controlUserName = circleService.getUserName(circleId, controlUser);
			logService.insertCircleLog(circleId, LogService.HANDLE.DELETE, LogService.CircleHandleField.CIRCLE_LABEL,controlUser,controlUserName,lab.getLabelName()) ;
		}

		return res ;
	}

	@Override
	public int addLabelToCircle(LabelEntity record,Long controlUserId) {
		record.setUserId(controlUserId);
		int res = labelMapper.insert(record);;
		if(res !=0) {
			String controlUserName = circleService.getUserName(record.getCircleId(), controlUserId);
			logService.insertCircleLog(record.getCircleId(), LogService.HANDLE.ADD, LogService.CircleHandleField.CIRCLE_LABEL,controlUserId,controlUserName,record.getLabelName()) ;
		}
		return res;
	}

	@Override
	public LabelEntity selectByPrimaryKey(Long labelId) {
		return labelMapper.selectByPrimaryKey(labelId);
	}

	@Override
	public Boolean existCircleLabelName(Long circleId, String labelName) {
		return labelMapper.existCircleLabelName(circleId,labelName);
	}

	@Override
	public Boolean existCircleLabel(Long circleId, Long labelId) {
		return labelMapper.existCircleLabel(circleId,labelId);
	}

	@Override
	public List<LabelEntity> selectByCircle(Long circleId, String labelNameFilter) {
		return labelMapper.selectByCircle(circleId,labelNameFilter);
	}

	@Override
	public int updateByPrimaryKeySelective(LabelEntity labelEntity, Long controlUser) {

		LabelEntity lab = selectByPrimaryKey(labelEntity.getLabelId());;
		int res = labelMapper.updateByPrimaryKeySelective(labelEntity) ;
		if(res !=0) {
			String controlUserName = circleService.getUserName(labelEntity.getCircleId(), controlUser);
			logService.insertCircleLog(labelEntity.getCircleId(), LogService.HANDLE.UPDATE, LogService.CircleHandleField.CIRCLE_LABEL,controlUser,controlUserName,labelEntity.getLabelName(),lab.getLabelName()) ;
		}
		return res;
	}

	/**
	 * 添加标签到任务
	 * @return
	 */
	@Override
	public int insertToTask(LabelTaskEntity labelTaskEntity, Long circleId ,Long controUser,String userFlag){
		labelTaskEntity.setUserId(controUser);
		int res = labelMapper.insertToTask(labelTaskEntity);
		if(res != 0) {
			String controlUserName = taskServer.getUserNameByTask(labelTaskEntity.getTaskId(), controUser);
			LabelEntity labebel = selectByPrimaryKey(labelTaskEntity.getLabelId());
			//圈子中任务的同步
			JSONObject json = new JSONObject();
			json.put("taskId",labelTaskEntity.getTaskId()) ;
			json.put("parentTaskId",taskServer.getParentTaskId(labelTaskEntity.getTaskId())) ;

			websocketService.sendCircle(controUser,circleId, WebsocketService.CirclePropertise.TASK_LABEL, WebsocketService.Control.ADD,json,userFlag);
			logService.insertTaskLog(labelTaskEntity.getTaskId(), LogService.HANDLE.ADD, LogService.TaskHandleField.TASK_LABEL,controUser,controlUserName,labebel.getLabelName()) ;
		}
		return res ;
	}

	/**
	 * 删除任务标签
	 * @return
	 */
	@Override
	public int deleteTaskLabel(Long taskId, Long labelId,Long circleId, Long controlUser,String userFlag){
		LabelEntity labebel = selectByPrimaryKey(labelId);
		int res = labelMapper.deleteTaskLabel(taskId, labelId);;
		if(res != 0) {
			String controlUserName = taskServer.getUserNameByTask(taskId, controlUser);
			//圈子中任务的同步
			JSONObject json = new JSONObject() ;
			json.put("taskId",taskId) ;
			json.put("parentTaskId",taskServer.getParentTaskId(taskId)) ;
			websocketService.sendCircle(controlUser,circleId, WebsocketService.CirclePropertise.TASK_LABEL, WebsocketService.Control.DELETE,json ,userFlag);
			logService.insertTaskLog(taskId, LogService.HANDLE.DELETE, LogService.TaskHandleField.TASK_LABEL,controlUser,controlUserName,labebel.getLabelName()) ;
		}
		return  res ;
	}

	/**
	 * 是否存在任务标签
	 * @return
	 */
	@Override
	public Boolean existTaskLabel(Long taskId, Long labelId){
		return labelMapper.existTaskLabel(taskId,labelId) ;
	}

}

